package com.mryin.performance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mryin.performance.mapper.TaskMapper;
import com.mryin.performance.pojo.Task;
import com.mryin.performance.service.TaskService;
import com.mryin.performance.utils.DataGridView;
import com.mryin.performance.utils.FormatDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.text.resources.cldr.zh.FormatData_zh_Hant_HK;

import javax.management.Query;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.SimpleFormatter;
import java.util.zip.DataFormatException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MRYIN
 * @since 2021-03-02
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private TaskService taskService ;

    /**
     *  已完成任务
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/FinishTask/{name}")
    public DataGridView getAllFinishTask(@PathVariable("name")String name ,
                                         @RequestParam("page")Integer pageNum,
                                         @RequestParam("limit")Integer pageSize,
                                         Model model,
                                         HttpSession session){
        List<Task> allFinishTask = taskMapper.getAllFinishTask(name);
        return new DataGridView(0,"查询成功",
                (long) allFinishTask.size(),allFinishTask);
    }

    /**
     *  未完成任务
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/UnFinishTask/{name}")
    public DataGridView getAllUnFinishTask(@PathVariable("name")String name ,
                                           @RequestParam("page")Integer pageNum,
                                           @RequestParam("limit")Integer pageSize){
        List<Task> allUnFinishTask = taskMapper.getAllUnFinishTask(name);
        return new DataGridView(0,"查询成功", (long) allUnFinishTask.size(),allUnFinishTask);
    }

    /**
     * 待完成任务
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/WaitFinishTask/{name}")
    public DataGridView getAllWaitFinishTask(@PathVariable("name")String name ,
                                             @RequestParam("page")Integer pageNum,@RequestParam("limit")Integer pageSize){
        List<Task> allWaitFinishTask = taskMapper.getAllWaitFinishTask(name);
        return new DataGridView(0,"查询成功",
                (long) allWaitFinishTask.size(),allWaitFinishTask);
    }

    /**
     *  超时完成任务
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/OverTimeFinish/{name}")
    public DataGridView getAllOverTimeFinishTask(@PathVariable("name")String name ,
                                                 @RequestParam("page")Integer pageNum,@RequestParam("limit")Integer pageSize){
        List<Task> allOverTimeFinishTask = taskMapper.getAllOverTimeFinishTask(name);
        return new DataGridView(0,"查询成功", (long) allOverTimeFinishTask.size(),allOverTimeFinishTask);
    }

    /**
     *  通过零件id来更新该任务的状态
     * @param id
     * @return
     */
    @RequestMapping("updateUnFinishTask")
    public DataGridView updateUnFinishTask(String id){
        System.out.println(id);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Task> id1 = queryWrapper.eq("id", id);
        Task task = taskMapper.selectOne(id1);
        task.setStatus(1);
        int i = taskMapper.updateById(task);
        System.out.println(i);
        return new DataGridView(0,"任务完成");
    }

    /**
     *  通过id查询改任务，并且设置最新完成时间和原因，并且将审核状态设置为0.
     *  0：待审核
     *  1：审核pass
     *  2：审核fail
     * @param id
     * @param finishTime
     * @param reason
     * @return
     */
    @RequestMapping("/UnFinishReason")
    public DataGridView unFinishReason(String id , String finishTime , String reason) {
        System.out.println(id);
        System.out.println(finishTime);
        System.out.println(reason);
        Date date = FormatDate.StringForDate(finishTime);
        Task task = taskMapper.getTaskById(id);
        task.setFinishTime(date);
        task.setReason(reason);
        task.setAuditingstatus(0);
        taskMapper.updateById(task);
        return  new DataGridView(0,"修改成功~~请尽快完成！！") ;
    }

    /**
     *  通过performanceIndex页面传递进来二维码的零件编号
     * @param session
     * @param id
     * @return
     */
    @RequestMapping("/QRCodeParam")
    public DataGridView reciveQRCodeParam(HttpSession session , String id){
        System.out.println("我是二维码的参数id==>"+id);
        session.setAttribute("id",id);
        return new DataGridView(0,"接收成功");
    }

    /**
     *  通过二维码传递进来的零件编号查询改变好的所有内容
     * @param session
     * @return
     */
    @RequestMapping("/TransmitQRCode")
    public DataGridView transmitQRCode(HttpSession session){
        String id = (String) session.getAttribute("id");
        System.out.println("我是传递过来的id==>"+id);
        List<Task> tasks = taskMapper.selectList(new QueryWrapper<Task>().eq("id", id));
        tasks.forEach(System.out::println);
        return new DataGridView(0,"查询成功",tasks);
    }

    /**
     *  给科长查看所有修改了完成时间的任务
     * @return
     */
    @RequestMapping("/selectAllTask")
    public DataGridView selectAllTask(){
        List<Task> tasks = taskMapper.selectList(new QueryWrapper<Task>().isNotNull("reason").eq("status", 0).orderByAsc("id"));
        return new DataGridView(0,"查询成功",tasks);
    }

    /**
     * 通过id找到该任务，设置审核人、审核时间。
     * 成功：赋值审核状态为1
     * @param id
     * @param username
     * @return
     */
    @RequestMapping("/auditingPass")
    public DataGridView auditingPass(String id , String username){
        System.out.println(id);
        System.out.println(username);
        Task task = taskService.getOne(new QueryWrapper<Task>().eq("id", id));
        task.setConfirmName(username);
        task.setConfirmTime(new Date());
        task.setAuditingstatus(1);
        taskService.updateById(task);
        return new DataGridView(0,"通过" + "，操作人：" + username);
    }

    /**
     *  通过id找到该任务，设置审核人、审核时间、审核失败原因
     *  失败：赋值审核状态为2
     * @param id
     * @param username
     * @param auditingreason
     * @return
     */
    @RequestMapping("/auditingFail")
    public DataGridView auditingFail(String id , String username , String auditingreason){
        System.out.println(id);
        System.out.println(username);
        System.out.println(auditingreason);
        Task task = taskService.getOne(new QueryWrapper<Task>().eq("id", id));
        task.setConfirmTime(new Date());
        task.setConfirmName(username);
        task.setAuditingstatus(2);
        task.setAuditingreason(auditingreason);
        taskService.updateById(task);
        return new DataGridView(0,"未通过" + "，操作人：" + username);
    }
}
